package com.potenza.training.reply.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.reply.dao.ReplyGroupDao;
import com.potenza.training.reply.entity.ReplyGroupEntity;
import com.potenza.training.reply.service.ReplyGroupService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("replyGroupService")
public class ReplyGroupServiceImpl extends ServiceImpl<ReplyGroupDao, ReplyGroupEntity> implements ReplyGroupService {
    @Autowired
    ReplyGroupDao replyGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<ReplyGroupEntity> replyGroupEntityQueryWrapper = new QueryWrapper<>();
        replyGroupEntityQueryWrapper.lambda().eq(ReplyGroupEntity::getIsStart,params.get("isStart"));

        IPage<ReplyGroupEntity> page = this.page(
                new Query<ReplyGroupEntity>().getPage(params),
                replyGroupEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Page<ReplyGroupEntity> findAll(Map map,Integer pageNum,Integer pageSize) {
        QueryWrapper<ReplyGroupEntity> replyGroupEntityQueryWrapper = new QueryWrapper<>();
        String replyName = (String) map.get("replyName");
        Integer isstart = (Integer) map.get("isStart");
        if(!StringUtils.isEmpty(replyName)){
            replyGroupEntityQueryWrapper.lambda().like(ReplyGroupEntity::getReplyName,replyName);
        }
        if(!StringUtils.isEmpty(isstart)){
            replyGroupEntityQueryWrapper.lambda().eq(ReplyGroupEntity::getIsStart,isstart);
        }
        replyGroupEntityQueryWrapper.lambda().orderByDesc(ReplyGroupEntity::getIsStart);
        Page<ReplyGroupEntity> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);
        return replyGroupDao.selectPage(page,replyGroupEntityQueryWrapper);
    }

    @Override
    public List<ReplyGroupEntity> findAlls() {
        QueryWrapper<ReplyGroupEntity> replyGroupEntityQueryWrapper = new QueryWrapper<>();
        return replyGroupDao.selectList(replyGroupEntityQueryWrapper);
    }

}